<template>
  <div
    class="login"
    style="background-image: url(https://guan-gai.com/login_bg.png)"
  >
    <div class="logo">
      <img src="../../assets/img/login/logo.png" alt="">
    </div>
    <div class="left" :id="leftStyle" :style="{display:imageWidth>0?'':'none'}">
      <img src="../../assets/img/login/login_image.png" alt="" />
    </div>
    <div class="right">
      <div class="login_form">
        <div class="title">冠盖智能能源管理平台</div>
        <a-form @submit="onSubmit" :form="form">
          <a-form-item>
            <div class="input_title">账户</div>
            <a-input
              autocomplete="autocomplete"
              size="large"
              placeholder="请输入账号"
              v-decorator="[
                'name',
                {
                  rules: [
                    {
                      required: true,
                      message: '请输入账户',
                      whitespace: true,
                    },
                  ],
                },
              ]"
            >
            </a-input>
          </a-form-item>
          <a-form-item>
            <div class="input_title">密码</div>

            <a-input
              size="large"
              placeholder="请输入密码"
              autocomplete="autocomplete"
              type="password"
              v-decorator="[
                'password',
                {
                  rules: [
                    { required: true, message: '请输入密码', whitespace: true },
                  ],
                },
              ]"
            >
            </a-input>
          </a-form-item>
          <a-form-item>
            <div class="input_title">验证码</div>
            <div class="code_box">
              <a-input
                size="large"
                placeholder="请输入验证码"
                v-decorator="[
                  'code',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入验证码',
                        whitespace: true,
                      },
                    ],
                  },
                ]"
              >
              </a-input>
              <div class="code_img"  @click="getCaptcha()">
                <img :src="captchaImg" alt="点击重试">
              </div>
            </div>
          </a-form-item>
          <!-- <div>
          <a-checkbox :checked="true">自动登录</a-checkbox>
          <a style="float: right">忘记密码</a>
        </div> -->
          <a-form-item>
            <a-button
              :loading="logging"
              style="width: 100%; margin-top: 24px"
              size="large"
              htmlType="submit"
              type="primary"
              >登录</a-button
            >
          <div class="Agreement">
            <span>登录即代表您已同意
              <a @click="goAgreement('privacy')">《冠盖隐私政策》</a>
              及
              <a @click="goAgreement('cookie')">《冠盖Cookie服务协议》</a>
              。</span>
          </div>
          </a-form-item>
        </a-form>
        <div class="copyright">版权所有©2023。冠盖雄关™”大数据预警平台</div>
      </div>
    </div>
  </div>
</template>

<script>
import { add,getRequest } from "@/services";
import { setAuthorization } from "@/utils/request";
import { loadRoutes } from "@/utils/routerUtil";
import { mapMutations } from "vuex";
import identify from "@/components/identify/identify.vue";
var _this;
export default {
  name: "Login",
  components: { identify },
  data() {
    return {
      logging: false,
      error: "",
      form: this.$form.createForm(this),
      code: "",
      leftStyle: "",
      imageWidth: "",
      captchaImg:'',
      captchaKey:''
    };
  },
  computed: {
    systemName() {
      return this.$store.state.setting.systemName;
    },
  },
  created() {
    _this = this
    this.getCaptcha()
    window.addEventListener("resize", this.handleResize);
  },
  beforeDestroy: function () {
    window.removeEventListener("resize", this.handleResize);
  },
  mounted() {
    this.handleResize();
    this.form.name = 'admin_admin'
    this.form.password = 'AdminPassGG'
  },
  methods: {
    ...mapMutations("account", ["setUser", "setPermissions", "setRoles"]),
    onSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err) => {
        if (!err) {
          this.logging = true;
          const name = this.form.getFieldValue("name");
          const password = this.form.getFieldValue("password");
          const key = this.captchaKey
          const code = this.form.getFieldValue("code").trim()
          add({ name: name, password: password,key:key,captcha:code }, "sessions/login").then(
            this.afterLogin
          ).catch(this.afterLogin);
        }
      });
    },
    getCaptcha(e){
      let arr = ['characters','math','flat']
      let type = arr[new Date().getTime() % arr.length]
      getRequest({},`verifyCode?type=${type}&by=captcha`).then(({data})=>{
        if(data.code==20000){
          _this.captchaKey=data.data.key
           _this.captchaImg=data.data.img
        }else{
        this.$message.error(data.message, 3);
        }
      })
    },
    goAgreement(e){
      window.open(`/#/Agreement?key=${e}`)
    },
    setCode(e) {
      this.code = e.toUpperCase();
      console.log(e);
    },
    handleResize() {
      let w = (document.documentElement.clientHeight - 30) * 0.7697;
      if (document.documentElement.clientWidth - w < 380) {
        this.leftStyle = "leftHide";
        this.imageWidth = 0;
      } else {
        this.leftStyle = "";
        this.imageWidth = w;
      }
    },
    afterLogin(res) {
      _this.logging = false;
      const loginRes = res.data;
      if (loginRes.code == 20000) {
        const { user, permissions, roles } = loginRes.data;
        this.setUser(user);
        this.setPermissions(permissions);
        this.setRoles(roles);
        setAuthorization({
          token: loginRes.data.token,
          expireAt:loginRes.data.tokenExpireAt || new Date(new Date().getTime()+604800000),
        });
        // 获取路由配置
        const routesConfig = [];
        loadRoutes(routesConfig);
        this.$router.push("/home");
        this.$message.success(`欢迎回来 ${user.name} `, 3);
      } else {
        this.getCaptcha()
        this.$message.error(loginRes.message, 3);
      }
    },
    onClose() {
      this.error = false;
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 100vw;
  height: 100%;
  display: flex;
  background-size: 100% 100%;
  justify-content: space-evenly;
  position: relative;
  .logo{
    position: absolute;
    left: 32px;
    top: 32px;
    width: 125px;
    height: 39px;
    img{
      width: 100%;
      height: 100%;
    }
  }
  .left {
    overflow: hidden;
    display: flex;
    align-items: center;
    img {
      width: 493px;
      height: 504px;
    }
  }
  #leftHide {
    width: 0;
    padding: 15px 0;
  }
  .right {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    .login_form {
      width: 420px;
      height: 533px;
      padding: 80px 48px;
      border-radius: 16px;
      background: #fff;
      position: relative;
      .Agreement{
        line-height: 1.3;
        margin-top: 8px;
      }
      .copyright {
        width: 100%;
        text-align: center;
        font-size: 11px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #ffffff;
        position: absolute;
        bottom: -30px;
        left: 0;
      }
      .title {
        line-height: 1;
        margin-bottom: 30px;
        width: 257px;
        font-size: 26px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #242c43;
        white-space: nowrap;
      }
      .input_title {
        font-size: 13px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #333333;
        line-height: 1;
        margin-bottom: 15px;
      }
      input {
        font-size: 11px;
      }
      .code_box {
        display: flex;
        .code_img {
          background: #ddd;
          height: 40px;
          min-width: 130px;
          border-radius: 3px;
          margin-left: 8px;
          overflow: hidden;
          img {
            width: 100%;
            height: 100%;
            border-radius: 3px;
          }
        }
      }
    }
  }
}
</style>
